package com.lovegu.server.infrastructure.modules.user.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.lovegu.server.client.dto.UserDTO;
import com.lovegu.server.client.vo.UserVO;
import com.lovegu.server.infrastructure.database.dos.UserDO;
import com.lovegu.server.infrastructure.database.mapper.UserMapper;
import com.lovegu.server.infrastructure.modules.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;

/**
 * @author 老顾
 * @description
 * @email: 1437594522@qq.com
 * @date 2022/11/19 10:30
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void addUser(UserDTO userDTO) {
        LambdaQueryWrapper<UserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserDO::getName, userDTO.getName());
        UserDO userDO = userMapper.selectOne(lambdaQueryWrapper);
        if (userDO != null){
            log.error("用户已存在");
            return;
        }
        UserDO user = UserDO.builder()
                .name(userDTO.getName())
                .age(userDTO.getAge())
                .email(userDTO.getEmail())
                .build();
        userMapper.insert(user);
    }

    @Override
    public void deleteUser(UserDTO userDTO) {
        userMapper.deleteById(userDTO.getId());
    }

    @Override
    public void deleteBatchUser(Collection<Integer> userDTOList) {
        userMapper.deleteBatchIds(userDTOList);
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        UserDO user = UserDO.builder()
                .id(userDTO.getId())
                .name(userDTO.getName())
                .age(userDTO.getAge())
                .email(userDTO.getEmail())
                .build();
        userMapper.updateById(user);
    }

    @Override
    public UserVO selectUser(UserDTO userDTO) {
        LambdaQueryWrapper<UserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserDO::getId, userDTO.getId());
        UserDO userDO = userMapper.selectOne(lambdaQueryWrapper);
        UserVO userVO = new UserVO();
        userVO.setName(userDO.getName());
        userVO.setAge(userDO.getAge());
        userVO.setEmail(userDO.getEmail());
        return userVO;
    }

    @Override
    public List<UserVO> selectBatchUser(UserDTO userDTO) {
        LambdaQueryWrapper<UserDO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(UserDO::getId, userDTO.getIds());
        List<UserDO> userDOList = userMapper.selectList(lambdaQueryWrapper);
        UserVO userVO = new UserVO();
        List<UserVO> userVOList = new ArrayList<>();
        for (UserDO userDO : userDOList) {
            userVO.setName(userDO.getName());
            userVO.setAge(userDO.getAge());
            userVO.setEmail(userDO.getEmail());
            userVOList.add(userVO);
        }
        return userVOList;
    }

    @Override
    public UserVO loginUser(UserDTO userDTO) {
        LambdaQueryWrapper<UserDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserDO::getName, userDTO.getName());
        // 根据名称查询用户
        UserDO userDO = userMapper.selectOne(lambdaQueryWrapper);
        UserDO user = UserDO.builder()
                .name(userDTO.getName())
                .password(userDTO.getPassword())
                .build();
        if (userDO == null){
            // 如果不存在则注册
            userMapper.insert(user);
        }
        // 存在则返回用户信息
        String uToken = UUID.randomUUID().toString();
        UserVO userVO = new UserVO();
        userVO.setName(user.getName());
        userVO.setToken(uToken);
        return userVO;
    }

}
